<?php 
if(KT!='KhiThe') exit;
class products_controller extends products_model{
		
	public function __construct()
	{
		global $lang;
		$this->lang = $lang;
		parent::__construct();
		parent::addObject('khithe_template');
		parent::addObject('khithe_request');
		parent::addObject('khithe_string');
		parent::addObject('khithe_paging');
		parent::addObject('khithe_upload');
	}
	
	public function index_action()
	{
				
		$limit=15;
		$resultHtml = NULL;
		$indent =NULL;
		$parentID = ($this->valUrl(1)==''?'All':$this->valUrl(1));
		$totalRows = mysql_num_rows(parent::getSqlProducts($parentID,'',''));
		$strPaging = $this->display_paging($totalRows, intval($this->valUrl(2)), intval($limit), url_base.'admin/products/index/'.$parentID.'', $this->lang,'/');
		$this->lang['paging'] = $strPaging;
		$this->lang['page'] = $this->valUrl(2); 
		$strHtmlContent = $this->khithe_display('index');
		$this->arrBlockHtml = $this->khithe_getBlockHtml($strHtmlContent);
		$this->lang['ComboxStatus']=$this->getCategory(0,0,0,$parentID,1);
		$resultProduct = parent::getSqlProducts( $parentID, intval($this->valUrl(2)), intval($limit) );
		while( $rowProduct = mysql_fetch_assoc($resultProduct) )
		{
			$arrProduct = array(
				"cell_ID"=>$rowProduct['ID'],
				"cell_1"=>'<input name="delId[]" type="checkbox" value="'.$rowProduct['ID'].'" />',
				"cell_2"=>"MaSP_".$rowProduct['ID'],
				"cell_3.1"=>$rowProduct['Title'],
				"cell_3.2"=>$rowProduct['Price']!=0?number_format($rowProduct['Price'],0,'.',','):'Call',
				"cell_4.1"=>'<a target="_new" href="'.url_base.'uploads/products/'.$rowProduct['Picture'].'">Xem</a>',
				"cell_5"=>$rowProduct['Status']=='true'?$this->lang['enabled']:$this->lang['disabled'],
				"class_cell"=>$rowProduct['Status']=='true'?"cell_groupitem$indent":"cell_groupitem_ex$indent",
			);
			$resultHtml .= $this->khithe_assign($this->arrBlockHtml['product'],$arrProduct);
		} mysql_free_result($resultProduct);
		$strHtmlContent = $this->khithe_assignBlock($strHtmlContent, array('htmlproduct'=>$resultHtml));
		$this->lang['dis'] ='';
		$this->lang['err_lang'] = '';
		if(empty($totalRows))
		{
			$this->lang['dis'] = 'disabled="disabled"';
			$this->lang['err_lang'] = '<h4 style="color:#F00">'.$this->lang['errnot'].'</h4>';
		}
		
		$strHtmlContent = $this->khithe_assign($strHtmlContent, $this->lang);
				
		return $strHtmlContent;
	}
	
	private function getCategory($ID, $Order, $indent, $selected, $stop)
	{	
		$indent +=1;
		$resultHtml = NULL; 
		$resultMenu = parent::getMenuProducts($ID);
		while( $rowMenu = mysql_fetch_assoc($resultMenu) )
		{
			$arrMenu = array(
				"cell_ID"=>$rowMenu['ID'],
				"cell_Value"=>str_repeat("&nbsp;&nbsp;", $indent).$rowMenu['Category'],
				"cell_selected"=>$selected==$rowMenu['ID']?' selected ':'',				
			);
			$resultHtml .= $this->khithe_assign($this->arrBlockHtml['category'],$arrMenu);
			if(isset($rowMenu['ID'])&&$stop==1) 
			$resultHtml .=$this->getCategory($rowMenu['ID'], $rowMenu['Order'], $indent, $selected, $stop);
			
		} mysql_free_result($resultMenu);
		
		return $resultHtml;
	}
	
	private function getCBoxManufacturer($selected)
	{		
		$strval = NULL;
		$resultManufacturer = parent::getManufacturer();
		while( $rowManu = mysql_fetch_assoc($resultManufacturer) )
		{
			$select = $rowManu['ID']==$selected?"selected=\"selected\"":"";
			$strval .= '<option '.$select.' value="'.$rowManu['ID'].'">'.$rowManu['Manufacturer'].'</option>';
		}
		
		return $strval;
	}
	
	public function add_action()
	{
		if($_GET['iframe']=='true')
		{
			$filename = 'khithe';
			if(!empty($_POST['txt_titleProduct'])){

                if(empty($_POST['txt_seo_key'])){
                    $_POST['txt_seo_key'] = $_POST['txt_titleProduct'];
                }

                if(empty($_POST['txt_seo_h1'])){
                    $_POST['txt_seo_h1'] = $_POST['txt_titleProduct'];
                }

                if(empty($_POST['txt_title_page'])){
                    $_POST['txt_title_page'] = $_POST['txt_titleProduct'];
                }

                if(empty($_POST['txt_description'])){
                    $_POST['txt_description'] = $_POST['txt_titleProduct'];
                }

				$_POST['Picture'] = empty($_FILES['file_picture']['name'])?'':$this->upload_img_multi($_FILES['file_picture'],path_base.'/uploads/products/',$filename,0,8000000);	
				$_POST['Picture_1'] = empty($_FILES['file_picture_1']['name'])?'':$this->upload_img_multi($_FILES['file_picture_1'],path_base.'/uploads/products/',$filename,1,8000000);
				$_POST['Picture_2'] = empty($_FILES['file_picture_2']['name'])?'':$this->upload_img_multi($_FILES['file_picture_2'],path_base.'/uploads/products/',$filename,2,8000000);
				$_POST['Picture_3'] = empty($_FILES['file_picture_3']['name'])?'':$this->upload_img_multi($_FILES['file_picture_3'],path_base.'/uploads/products/',$filename,3,8000000);
				$_POST['Picture_4'] = empty($_FILES['file_picture_4']['name'])?'':$this->upload_img_multi($_FILES['file_picture_4'],path_base.'/uploads/products/',$filename,4,8000000);


				if(parent::mdcheckSeoKey($_POST['txt_seo_key'])>0)
				{
					echo '<script type="text/javascript">alert("'.$this->lang['sms_same_key'].'");</script>';
				}
				else{				
					$finish = parent::updateInsertProduct($_POST);
				}
				if($finish){
					$linkSitemap='sitemap.xml';
					$fileXML = simplexml_load_file($linkSitemap);
					$urlTab = $fileXML->addChild("url");
					$urlTab->addChild("loc",url_base.(parent::seo_list_parent(products_menu,NULL,trim($_POST['cb_select']))).$_POST['txt_seo_key'].'.html');
					$urlTab->addChild("changefreq",'always');
					$urlTab->addChild("priority",'0.9');
					
					$fileXML->asXML($linkSitemap);	
					return '<script type="text/javascript">window.parent.location.reload();</script>';
				}
			}
			
			/*Seo*/
			$this->lang['val_seo_key'] ='';
			$this->lang['val_title_page'] ='';
			$this->lang['val_keywords'] = '';
			$this->lang['val_description'] ='';
			$this->lang['val_seo_h1'] ='';
			$this->lang['val_seo_h2'] ='';
			$this->lang['val_seo_p'] ='';
			
			$this->lang['txt_productValue'] ='';
			$this->lang['file_pictureValue'] ='';
			$this->lang['txt_priceValue'] ='';
			$this->lang['txt_priceSpecialValue'] ='';
			$this->lang['txt_promotionValue'] ='';
			$this->lang['txt_warrantyValue'] ='';
			$this->lang['txt_detailsValue'] ='';
			$this->lang['txt_SpecialValue'] ='';
			$this->lang['txt_FromValue'] ='';
			$this->lang['txt_ToValue'] ='';
			$this->lang['checked'] ='';
			$this->lang['acProduct'] ='add';
			$this->lang['manufacturer'] = $this->getCBoxManufacturer(0);
			$this->lang['titlePageEditor'] = $this->lang['titleProductEditor'];
			$this->lang['txt_textarea'] = '';
			$strHtmlContent = $this->khithe_display('editor');
			$this->arrBlockHtml = $this->khithe_getBlockHtml($strHtmlContent);
			$resultHtml = $this->getCategory(0,'',0,0,1);
			$strHtmlContent = $this->khithe_assignBlock($strHtmlContent, array('htmlCat'=>$resultHtml));
			$strHtmlContent = $this->khithe_assign($strHtmlContent, $this->lang);
			return $strHtmlContent;
		}
	}
	
	public function edit_action()
	{
		if($_GET['iframe']=='true')
		{
			if(!empty($_POST['txt_titleProduct'])){
				$filename = 'khithe';
				$_POST['Picture'] = $this->upload_img_multi($_FILES['file_picture'],path_base.'/uploads/products/',$filename,0,8000000);	
				$_POST['Picture_1'] = $this->upload_img_multi($_FILES['file_picture_1'],path_base.'/uploads/products/',$filename,1,8000000);
				$_POST['Picture_2'] = $this->upload_img_multi($_FILES['file_picture_2'],path_base.'/uploads/products/',$filename,2,8000000);
				$_POST['Picture_3'] = $this->upload_img_multi($_FILES['file_picture_3'],path_base.'/uploads/products/',$filename,3,8000000);
				$_POST['Picture_4'] = $this->upload_img_multi($_FILES['file_picture_4'],path_base.'/uploads/products/',$filename,4,8000000);			
				
				if(parent::mdcheckSeoKey($_POST['txt_seo_key'])>0 && $_POST['txt_seo_key'] != $_POST['txt_seo_key_hi'])
				{
					echo '<script type="text/javascript">alert("'.$this->lang['sms_same_key'].'");</script>';
				}
				else{
					$finish = parent::updateInsertProduct($_POST);
				}
				if($finish)
				{					
					return '<script type="text/javascript">window.parent.location.reload();</script>';
				}
			}
			$row = parent::selectProduct($this->valUrl(1));
			/*Seo*/
			$this->lang['val_seo_key'] =$row['SeoKey'];
			$this->lang['val_title_page'] =$row['TitlePage'];
			$this->lang['val_description'] =$row['Description'];
			$this->lang['val_keywords'] =$row['KeyWords'];
			$this->lang['val_seo_h1'] =$row['SeoH1'];
			$this->lang['val_seo_h2'] =$row['SeoH2'];
			$this->lang['val_seo_p'] =$row['SeoP'];
			
			$this->lang['txt_productValue'] = $row['Title'];
			$this->lang['file_pictureValue'] =$row['Picture'];
			$this->lang['txt_priceValue'] =$row['Price'];
			$this->lang['txt_priceSpecialValue'] =$row['PriceSpecial'];
			$this->lang['txt_promotionValue'] =$row['Promotion'];
			$this->lang['txt_warrantyValue'] =$row['Warranty'];
			$this->lang['txt_SpecialValue'] =$row['Special'];
			$this->lang['txt_FromValue'] =$row['From']!=''?date("d-m-Y",$row['From']):'';
			$this->lang['txt_ToValue'] =$row['To']!=''?date("d-m-Y",$row['To']):'';
			$this->lang['valID'] =$row['ID'];
			$this->lang['checked'] = $row['Special']=='true'?'checked="checked"':'';
			$this->lang['acProduct'] ='edit';
			$this->lang['manufacturer'] = $this->getCBoxManufacturer($row['ManufacturerID']);
			$this->lang['titlePageEditor'] = $this->lang['titleProductEdit'];
			$this->lang['txt_textarea'] = $row['Details'];			
			$strHtmlContent = $this->khithe_display('editor');
			$this->arrBlockHtml = $this->khithe_getBlockHtml($strHtmlContent);
			$resultHtml = $this->getCategory(0,'',0,$row['CategoryID'],1);
			$strHtmlContent = $this->khithe_assignBlock($strHtmlContent, array('htmlCat'=>$resultHtml));
			$strHtmlContent = $this->khithe_assign($strHtmlContent, $this->lang);
			
			return $strHtmlContent;
		}
	}
	
	public function index_ajax_action()
	{
		switch ($this->valUrl(3))
		{
			case 'change':
			
				return $this->index_action();
				
			break;
			
			case 'open':
				$arr = explode(',',$this->valUrl(4));
				foreach ( $arr as $ID ){
					$finish = parent::setCheckedProducts($ID,'true');
				}
			break;
			
			case 'close':
				$arr = explode(',',$this->valUrl(4));
				foreach ( $arr as $ID ){
					$finish = parent::setCheckedProducts($ID,'false');
				}
			break;
			
			case 'del':
				$arr = explode(',',$this->valUrl(4));
				foreach ( $arr as $ID ){
					$finish = parent::delCheckedProducts($ID);
				}				
			break;
			
		}
		if($finish)
			return $this->index_action();
		else
			return 'notOk';		
	}


}
?>